/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.unmsm.fisi.ceiecbt.app.servlets;

import java.io.*;
import java.net.*;

import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;

import pe.edu.unmsm.fisi.ceiecbt.ejb.beans.PortafolioInversionBean;
import pe.edu.unmsm.fisi.ceiecbt.ejb.beans.PortafolioInversionRemote;
import pe.edu.unmsm.fisi.ceiecbt.ejb.beans.ComtbUsuarioFacadeRemote;
import pe.edu.unmsm.fisi.ceiecbt.ejb.entidad.ComtbUsuario;


/**
 *
 * @author Liz Alvarez
 */
public class Session extends HttpServlet {
   
    @EJB
      private PortafolioInversionRemote pr;
    
    @EJB
      private ComtbUsuarioFacadeRemote ur;
    
    
    ComtbUsuario comtbusu;
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String usuario=request.getParameter("email");
            String password=request.getParameter("password");

          //if(usuario.equals("liz") && password.equals("123")){
            if(ur.validaByUsuClav(usuario, usuario)==1){  
            
            HttpSession sesion_act=request.getSession(true);
            sesion_act.setAttribute("user", usuario);
            sesion_act.setAttribute("PortafolioInversionBean", pr);
            //sesion_act.setAttribute("GestionPortafolioBean", gpr);
            response.sendRedirect("Inicio");
            
            }
            else{
            response.sendRedirect("Inicio");
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
